package com.atguigu.vod.controller;

import com.atguigu.common.Result;
import com.atguigu.serviceBase.exception.GuliException;
import com.atguigu.vod.service.VodService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 视频管理Controller
 *
 * @author Summerday
 */

@RestController
@RequestMapping("/eduVod/video")
public class VodController {

    @Resource
    private VodService vodService;

    /**
     * 上传视频到阿里云
     */

    @PostMapping("/uploadAliyun")
    public Result uploadVideo(@RequestBody MultipartFile file) {

        //返回上传视频的id
        String videoId = vodService.uploadVideo(file);

        return Result.ok().data("videoId", videoId);
    }

    /**
     * 根据视频id删除阿里云视频
     */
    @DeleteMapping("/{videoSourceId}")
    public Result removeVideo(@PathVariable("videoSourceId") String videoSourceId) {
        vodService.removeVideo(videoSourceId);
        return Result.ok();
    }

    /**
     * 删除多个视频
     */
    @DeleteMapping("/batch")
    public Result removeVideoList(@RequestParam("videoList")List<String> videoIdList){
        vodService.removeVideoList(videoIdList);
        return Result.ok();
    }

    /**
     * 根据视频id获取视频的播放凭证
     */
    @GetMapping("/getPlayAuth/{id}")
    public Result getPlayAuth(@PathVariable String id){
        String playAuth = vodService.getPlayAuth(id);
        if(playAuth == null){
            throw new GuliException("获取凭证失败");
        }
        return Result.ok().data("playAuth",playAuth);
    }
}
